﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AfterlifeLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using AfterlifeLib.Collision;

namespace Afterlife.Entities
{
    /// <summary>
    /// De staat waarin de zombie verkeerd.
    /// </summary>
    public enum ZombieState
    {
        Unaware,
        Aware,
    }

    public class Zombie : Entity
    {
        const float maxTurn = 10f;

        Vector2 target = Vector2.Zero;
        /// <summary>
        /// Het doel waar de zombie naartoe probeer te lopen
        /// </summary>
        public Vector2 Target
        {
            get { return target; }
            set { target = value; }
        }

        public Zombie(World world, Vector2 position)
            : base(world)
        {
            Position = position;
        }

        public override void LoadContent(ContentManager content)
        {
            Texture = content.Load<Texture2D>("Images/Sprites/zombie");
            Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
            Speed = 30f;
        }

        public override void Update(GameTime gameTime)
        {
            float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

            float dx = target.X - (Position.X + Origin.X);
            float dy = target.Y - (Position.Y + Origin.Y);
            Rotation = (float)Math.Atan2(dy, dx);

            Velocity = new Vector2((float)Math.Cos(Rotation), (float)Math.Sin(Rotation));

            Velocity = (Velocity * Speed) * delta;

            if (Math.Abs(dx) < 32 && Math.Abs(dy) < 32)
                Velocity = Vector2.Zero;

            Position = CollisionHelper.GetNewPosition(GetWorld<AfterlifeWorld>().CurrentLevel.CollisionLayer, Position, Velocity, Texture.Width, Texture.Height);
        }

        public override void Draw(SpriteBatch spriteBatch, Camera2D camera)
        {
            spriteBatch.Draw(
                    Texture,
                    Position - camera.Position + Origin,
                    null,
                    Color.White,
                    Rotation + MathHelper.PiOver2,
                    Origin,
                    1,
                    SpriteEffects.None,
                    0);
        }
    }
}
